<template>
  <div>
    <div v-show="!isShowDetail">
      <jxf-titletip :title="title" @refreshPage="refreshPage"></jxf-titletip>
      <el-card class="mt20">
        <el-form :inline="true" :model="searchInfo" ref="searchInfo">
          <el-form-item label="采购商企业名称:" prop="enterpriseName">
            <el-input ref="enterpriseName" v-model="searchInfo.enterpriseName"
                      @keyup.enter.native="$refs['status'].focus()" clearable></el-input>
          </el-form-item>
          <el-form-item label="固定审核结果:" prop="status">
            <el-select v-model="searchInfo.status" clearable placeholder="全部">
              <el-option ref="status" v-for="item in statusList" :key="item.value" :label="item.label"
                         :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <jxf-Date :startTime.sync="searchInfo.startTime" :endTime.sync="searchInfo.endTime"
                    label="申请时间：" ></jxf-Date>
          <el-form-item>
            <el-button type="primary" @click="onSearch()">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="resetForm('searchInfo')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card class="jxf-table-card mt20">
        <div slot="header" class="clearfix">
          <span>数据列表</span>
          <div>
            <el-button type="primary" @click="exportInfo" :loading="isSuccess">导出</el-button>
          </div>
        </div>
        <jxf-table class="" :table="dataTable">
          <template slot="status" slot-scope="scope">
            <span>{{scope.row.status === 0 ? '待审核': scope.row.status === 1 ? '通过' :  scope.row.status === 2 ? '驳回' : ''}}</span>
          </template>
          <template slot="operation" slot-scope="scope">
            <el-button type="text" @click="showDetail(scope.row)">查看详情</el-button>
            <el-button v-if="scope.row.status === 0" type="text" @click="audit(scope.row)">审批</el-button>
          </template>
        </jxf-table>
      </el-card>
      <jxf-pagination class="mt20" :pageTotal="pageTotal" :currentPage.sync='currentPage' :pageSize.sync="pageSize"
                      @currentPageChange="currentPageChange" @pageSizeChange="pageSizeChange"></jxf-pagination>
    </div>
    <ApplyAudit :isShowAudit.sync="isShowAudit" :auditInfo="auditInfo" @refreshCurrentPage="getList"></ApplyAudit>
    <MerchantReview v-if="isShowDetail"  :title="subTitle" @goBack="goBack" :dataInfo="subDataInfo" @goPrewView="goPrewView" @fresh="fresh"></MerchantReview>
  </div>
</template>

<script>
  import formatDate from '@/assets/js/formatDate'
  import ApplyAudit from './components/ApplyAudit'
  import MerchantReview from './../Role/components/MerchantReview'
  export default {
    name: "EmporaryQuotaApplication",
    components: {ApplyAudit, MerchantReview},
    data() {
      return {
        title: "临时额度申请",
        statusList: [{
          label: "待审核",
          value: 0
        }, {
          label: "通过",
          value: 1
        }, {
          label: "驳回",
          value: 2
        }],
        searchInfoBack: {
          enterpriseName: "",
          status: "",
          startTime: "",
          endTime: "",
          type:1 //临时额度
        },
        searchInfo: {
          enterpriseName: "",
          status: "",
          startTime: "",
          endTime: "",
          type:1
        },
        isSuccess: false,
        dataTable: {},
        currentPage: 1,
        pageSize: 10,
        pageTotal: 0,
        isShowDetail: false,
        isShowAudit: false,
        auditInfo:{},
        subTitle: "",
        subDataInfo: {}
      }
    },
    methods: {
      // 返回当前页面
      goPrewView() {
        this.isShowDetail = false
        this.getData()
      },
      fresh() {
        this.getList()
      },
      init() {
        this.dataTable = {
          border: true,
          loading: true,
          tr: [
            {
              id: '1',
              label: '采购商企业名称',
              minWidth: "130",
              prop: 'enterpriseName'
            },
            {
              id: '2',
              label: '申请时间',
              minWidth: "130",
              prop: 'applyTime'
            },
            {
              id: '3',
              label: '采购商主体账号',
              minWidth: "130",
              prop: 'tel'
            },
            {
              id: '4',
              label: '所在城市',
              minWidth: "130",
              prop: 'cityName'
            },
            {
              id: '5',
              label: '审核状态',
              show: 'template',
              minWidth: "130",
              prop: 'status'
            },
            {
              id: '9',
              label: '操作',
              show: 'template',
              minWidth: "100",
              prop: 'operation',
              fixed: "right"
            }
          ],
          data: []
        }
      },
      // 详情 --> 列表
      goBack() {
        this.isShowDetail = false;
      },
      getList() {
        let info = formatData.clearObjEmptyPrama({...this.searchInfo, pageSize: this.pageSize, pageNum: this.currentPage})
        ajax.post(apis.factory.getCreditApplylist, info)
          .then(res => {
            if (res.success === true) {
              this.pageTotal = res.data.total
              if (res.data.dataList !== [] && res.data.dataList !== null) {
                this.dataTable.data = this.formatData(res.data.dataList)
              } else {
                this.dataTable.data = []
              }
              this.dataTable.loading = false
            }
          })
      },
      // 列表数据处理
      formatData(list) {
        return list.map(item => {
          return {
            id: item.id,
            userId: item.userId,
            merchantId: item.merchantId,
            enterpriseName: item.enterpriseName,
            applyTime: formatDate.dateFormat(item.applyTime),
            tel: item.tel,
            cityName: item.cityName,
            status: item.status,
            applyCredit: item.applyCredit
          }
        })
      },
      // 查看详情
      showDetail(obj) {
        this.subTitle = "采购商详情";
        let data = {
          id: obj.merchantId,
          statusNum: 1,
          status: obj.status,
          userId: obj.userId
        };
        ajax.post(apis.user.merchantReview, data).then(res => {
          if(res.code === "10000000") {
            this.subDataInfo = {
              status: 0,
              page: 1,
              telphone: obj.telphone,
              accountStatus: res.data.merchantPcVo.userStatus,
              businessId: res.data.merchantPcVo.businessId,
              identityCard: {
                value: res.data.merchantPcVo.identityCardImage ? res.data.merchantPcVo.identityCardImage : "",
                isEdit: false
              },
              license: {
                value: res.data.merchantPcVo.licenseImage ? res.data.merchantPcVo.licenseImage : "",
                isEdit: false
              },
              cityId: res.data.merchantPcVo.cityId,
              otherQualification: {
                value: res.data.merchantPcVo.otherQualification ? res.data.merchantPcVo.otherQualification : "",
                isEdit: false
              },
              merchantId: obj.userId,
              id: res.data.merchantPcVo.id,
              enterpriseName: obj.enterpriseName,
              enterpriseAuthId: res.data.merchantPcVo.enterpriseAuthId,
              logo: res.data.merchantPcVo.logo ? res.data.merchantPcVo.logo : "",
              reTimeStart: res.data.merchantPcVo.reTimeStart,
              reTimeEnd: res.data.merchantPcVo.reTimeEnd,
              identityCardStart: res.data.merchantPcVo.certificateValidity ? res.data.merchantPcVo.certificateValidity.split('-')[0] : "",
              identityCardEnd: res.data.merchantPcVo.certificateValidity ? res.data.merchantPcVo.certificateValidity.split('-')[1] : "",
              deliveryIds: res.data.merchantPcVo.deliveryIds,
              userId: res.data.merchantPcVo.userId,
              merchantInfo: [
                {
                  label: "*采购商主体账号",
                  value: res.data.merchantPcVo.telphone,
                  type: "telphone"
                }, {
                  label: "审核状态",
                  value: res.data.merchantPcVo.status,
                  type: 'status'
                }, {
                  label: "*企业名称",
                  value: res.data.merchantPcVo.enterpriseName,
                  type: 'enterpriseName',
                  isEdit: false
                },  {
                  label: "所属分公司",
                  value: res.data.merchantPcVo.agentName,
                  type: "Branch"
                }, {
                  label: "客服电话",
                  value: res.data.merchantPcVo.contact,
                  type: 'contact',
                  isEdit: false
                }, {
                  label: "对接业务员",
                  value: res.data.merchantPcVo.salesmanName,
                  type: 'salesmanName'
                }, {
                  label: "*经营地址",
                  value: res.data.merchantPcVo.detailAdress,
                  type: 'detailAdress',
                  isEdit: false
                }, {
                  label: "所属渠道商",
                  value: res.data.merchantPcVo.channelDealerName,
                  type: 'channelDealerName'
                }, {
                  label: "*社会代码",
                  value: res.data.merchantPcVo.creditCode,
                  type: "creditCode",
                  isEdit: false
                }, {
                  label: "",
                  value: ""
                }
              ],
              otherInfo: [{
                label: "*注册主体人的名称",
                value: res.data.merchantPcVo.registrantName,
                type: "subjectPerson",
                isEdit: false
              }, {
                label: "*注册主体人身份证号码",
                value: res.data.merchantPcVo.registrantIdNum,
                type: "registeredIDNumber",
                isEdit: false
              }, {
                label: "*法人名称",
                value: res.data.merchantPcVo.legalName,
                type: "legalName",
                isEdit: false
              }, {
                label: "*法人身份证号码",
                value: res.data.merchantPcVo.idNumber,
                type: "identityCard",
                isEdit: false
              }, {
                label: "*法人身份证有效期",
                value: res.data.merchantPcVo.certificateValidity,
                type: "certificateValidity",
                isEdit: false
              }, {
                label: "入驻时间",
                value: res.data.merchantPcVo.auditTime,
                type: "auditTime"
              }, {
                label: "南京银行子账号",
                value: "",
                type: "legalIDNumber"
              }, {
                label: "华夏银行子账号",
                value: "",
                type: "legalIDNumber"
              }, {
                label: "预留银行卡所属行",
                value: "",
                type: "legalIDNumber",
                isEdit: false
              }, {
                label: "银行卡卡号",
                value: res.data.merchantPcVo.cardNo,
                type: "cardNo"
              }],
              originalinfo: {

              },

            };
            this.isShowDetail = true
          } else {
            this.$message.error(res.message)
          }
        })
      },
      // 发放
      audit(row){
        this.isShowAudit = true
        this.auditInfo = {
          type:1, //固定额度
          applyId:row.id,
          grantCredit: row.applyCredit,
          enterpriseName: row.enterpriseName
        }
      },
      // 导出
      exportInfo(){
        this.isSuccess = true;
        let info = formatData.clearObjEmptyPrama({...this.searchInfo, pageSize: this.pageSize, pageNum: this.currentPage})
        ajax.export(apis.factory.exporttempcreditlist, info)
          .then(res => {
            this.isSuccess = false
          })
      },
      onSearch() {
        this.currentPage = 1
        this.getList()
      },
      // 搜索表单重置
      resetForm(formName) {
        // this.$refs[formName].resetFields();
        this.searchInfo = {...this.searchInfoBack}
      },
      // 切换页面
      currentPageChange(pageNo) {
        this.currentPage = pageNo
        this.getList()
      },
      // 切换页面条数
      pageSizeChange(pageNo) {
        this.pageSize = pageNo
        this.getList()
      },
      // 刷新页面
      refreshPage() {
        this.currentPage = 1
        this.pageSize = 10
        this.searchInfo = {...this.searchInfoBack}
        this.getList()
      }
    },
    created() {
      this.init()
      this.getList()
    }
  }
</script>

<style scoped>

</style>
